package com.shujia.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

public class Code04GetChild {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper("master:2181",
                15 * 1000,
                new Watcher() {
                    @Override
                    public void process(WatchedEvent event) {
                        System.out.println("process函数执行了...");
                    }
                }
        );

        String path = "/api";
        Stat exists = zooKeeper.exists(path, false);

//        System.out.println(exists);
        if (exists != null) {
            // 获取数据

            if (exists.getNumChildren() > 0){
                List<String> children = zooKeeper.getChildren(path, false);
                for (String child : children) {
                    System.out.println(child);
                }
            }else {
                System.out.println("当前节点不存在有子节点..");
            }
        } else {
            System.out.println("当前给定节点不存在...");
        }

        // 如何获取当前给定路径下所有节点的子节点路径 ？ 例如：
            //  /api/node1
            //  /api/node1/node11

        zooKeeper.close();


    }
}
